﻿/*--------------------------------------------------------------
 * <copyright file="EngineeringLanguagePer.cs" company="O-RID">
 * Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei ZhangJun</author>
 * <summary> 
 *          工程別言語別生産性 查询
 *          作成日：08/10/2010
 *  </summary>
 *  ------------------------------------------------------------
*/
namespace Orid.Management.Statistics.OperatorUnitMangement.Command.Matter
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.MatterQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 工程別言語別生産性 查询
    /// </summary>
    public class EngineeringLanguagePer : Orid.Management.Statistics.OperatorUnitMangement.Command.ICommand
    {
        #region 变量定义
        /// <summary>
        /// 查询区域对象 Container
        /// </summary>
        private MatterSelContainer container;

        /// <summary>
        /// 错误消息提示 ErrorMessagebox
        /// </summary>
        private childWindow.MessageBox errorMessbox;

        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient;

        /// <summary>
        /// 查询服务定义 WCFMatterCorrelationClient
        /// </summary>
        private WCFServers.MatterQueryRef.WCFMatterCorrelationClient wcfMatterCorrelationClient;

        #endregion  变量定义

        /// <summary>
        /// 工程別言語別生産性 查询
        /// </summary>
        /// <param name="container">查询区域对象 container</param>
        public EngineeringLanguagePer(MatterSelContainer container)
        {
            this.container = container;

            // MatterSelContainer 的参数输入框,不显示
            this.container.tbEnterPar.Visibility = Visibility.Collapsed;
            this.container.tbSelContext.Width = 360;

            this.InitField();
        }

        /// <summary>
        /// 查询完成事件 ExecOver
        /// </summary>
        public event EventHandler<SelectEngineeringLanguageProQueryInforCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;

        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfMatterCorrelationClient = Wcf.Service.MatterQuery;

            this.errorMessbox = new childWindow.MessageBox();

            this.wcfLogClient = Wcf.Service.Log4;

            this.wcfMatterCorrelationClient.SelectEngineeringLanguageProQueryInforCompleted += new EventHandler<SelectEngineeringLanguageProQueryInforCompletedEventArgs>(this.OnExecover);
        }

        /// <summary>
        /// 查询结束事件 OnExecOver
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of SelectOPScoreDetailCompletedEventArgs</param>
        protected void OnExecover(object sender, SelectEngineeringLanguageProQueryInforCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of EventArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }

        #region ICommand 成员

        /// <summary>
        /// 工程別言語別生産性 
        /// 命令的具体实现 
        /// 实现接口ICommand
        /// </summary>
        public void Exec()
        {

            try
            {
                int error = FunctionClass.IsSelectDo(
                                                   this.container.dpDateFrom,
                                                   this.container.dpDateTo,
                                                   this.container.tbSelContext,
                                                   this.errorMessbox);

                if (error == 0)
                {
                    this.container.btnSelList.IsEnabled = true;
                    return;
                }

                // 传送到服务端的参数列表
                WCFServers.MatterQueryRef.WhereParameters fitWhereData = new WhereParameters();

                try
                {
                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.container.dpDateFrom.Text))
                    {
                        //判断时间
                        DateTime startTime = Convert.ToDateTime(this.container.tpTimeStart.Value != null ? this.container.tpTimeStart.Value.ToString() : "06:00");

                        fitWhereData.WhereDateFrom = this.container.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd") + " " + startTime.ToString("HH:mm");
                    }
                    else
                    {
                        fitWhereData.WhereDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.container.dpDateTo.Text))
                    {
                        //判断时间
                        DateTime startEnd = Convert.ToDateTime(this.container.tpTimeEnd.Value != null ? this.container.tpTimeEnd.Value.ToString() : "06:00");

                        fitWhereData.WhereDateTo = this.container.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd") + " " + startEnd.ToString("HH:mm");
                    }
                    else
                    {
                        fitWhereData.WhereDateTo = "null";
                    }

                    //LINE选择条件
                    if (ClientDataList.SelLineList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelLineList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        fitWhereData.WhereServer = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }

                    //两个日期相差的天数
                    fitWhereData.DateDiffs = FunctionClass.DateDiff(
                        //this.container.dpDateFrom.SelectedDate.Value, 
                        //this.container.dpDateTo.SelectedDate.Value
                                    Convert.ToDateTime(fitWhereData.WhereDateFrom),
                                    Convert.ToDateTime(fitWhereData.WhereDateTo)
                                   );

                    this.container.startDataTime = Convert.ToDateTime(fitWhereData.WhereDateFrom);
                }
                catch 
                {
                    throw new Exception("转换查询条件出错！");
                }

                this.OnExecBegin(null, null);

                //工程別言語別生産性
                this.wcfMatterCorrelationClient.SelectEngineeringLanguageProQueryInforAsync(fitWhereData);

                //查询按钮不可用
                this.container.btnSelList.IsEnabled = false;
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + "工程別言語別生産性 接口实现类 EngineeringLanguagePer " + ex.Message.ToString());

                //查询按钮可用
                this.container.btnSelList.IsEnabled = true;

                FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "提示",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }
        #endregion
    }
}
